package com.imy.luascript.app.ui.main.fragment.project;

import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.base.adapter.BaseDelegate;
import com.imy.common.constant.Constant;
import com.imy.common.util.ALog;
import com.imy.luascript.R;
import com.imy.luascript.app.data.general.ProjectItem;
import com.imy.luascript.app.ui.main.MainActivity;
import com.imy.luascript.app.ui.precode.PreViewCodeActivity;
import com.imy.luascript.app.ui.dialog.createproject.CreateProjectDialog;
import com.imy.luascript.app.ui.preview.ImagePreviewActivity;
import com.imy.luascript.base.fragment.BaseListFragment;
import com.imy.luascript.event.ProjectEvent;

import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ProjectFragment extends BaseListFragment<ProjectViewModel, ProjectAdapter> {
    private final String PROJECT_PATH = Constant.PROJECT_PATH;

    @Override
    protected void onLazyLoadData() {
        mViewModel.setOnScanFileListener(list -> {
            if (list.size() == 0) {
                loadCompleted();
                showNoData("当前无项目工程，请创建", R.mipmap.icon_add_project, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //展示创建工程的dialog
                        showCreateDialog();
                    }
                });
                return;
            }
            loadList(list);
            adapter.showNoMore();
        });
        mViewModel.loadData(PROJECT_PATH);

    }

    @Override
    protected void initView(View contentView) {

        super.initView(contentView);
        adapter.getProjectDelegate().setOnItemClickListener(new BaseDelegate.ItemClickListener<ProjectItem>() {
            @Override
            public void onItemClicked(int position, ProjectItem projectItem, View view) {
                //进入下一级
                File file = projectItem.file;
                ALog.i("当前点击的file:"+file.getAbsolutePath());
                if (file.isDirectory()){
                    mViewModel.loadData(file.getAbsolutePath());
                }else if (file.getName().endsWith(".png")
                || file.getName().endsWith(".jpg")){
                    ImagePreviewActivity.start(self,file.getAbsolutePath());
                }else {
                    PreViewCodeActivity.start(self,file.getAbsolutePath(),file.getName());

                }
            }
        });
        adapter.getBackDelegate().setOnItemClickListener((BaseDelegate.ItemClickListener<ProjectItem>) (position, o, view) -> {
            String absolutePath = o.file.getParentFile().getAbsolutePath();
            mViewModel.loadData(absolutePath);
        });
    }

    @Override
    protected boolean isBindEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(ProjectEvent event) {
        if (event.getEvent() == ProjectEvent.CREATE_PROJECT) {
            hideNoData();
            mViewModel.loadData(PROJECT_PATH);
        }else if (event.getEvent()==ProjectEvent.REFRESH){
            hideNoData();
            mViewModel.loadData();
        }
    }

    private void showCreateDialog() {
        new CreateProjectDialog((AppCompatActivity) self);
    }

    @Override
    protected ProjectViewModel getViewModel() {
        return new ProjectViewModel(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_project;
    }

    @Override
    protected ProjectAdapter createAdapter() {
        return new ProjectAdapter(self, new ArrayList<>());
    }
}
